package com.bw.packcar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.packcar.adapter.PackAdapter

import com.bw.packcar.model.PackEntity
import com.bw.packcar.net.CarPackViewModel
import com.bw.packcar.databinding.ActivityCarPackBinding
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb


/**
 * 智慧园区-车位管理
 * 车位管理
 * 兰宏宇
 * 20250512
 */
@Route(path = RoutePath.PATH_PACK_CAR)
class CarPackActivity : BaseMvvmActivity<ActivityCarPackBinding, CarPackViewModel>() {

    private lateinit var pAdapter: PackAdapter
    private var pList: MutableList<PackEntity> = mutableListOf()
    private var deletePosition:Int = -1

    override fun onResume() {
        super.onResume()
        vm.getCarPackByArea(1)
    }

    override fun initView() {
        pAdapter = PackAdapter()
        binding.rv.adapter = pAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)


        binding.include.ivBack.setOnClickListener {
            finish()
        }

        binding.include.tvTitle.text = "车位管理"
        binding.include.ivAdd.visibility = View.VISIBLE
        binding.include.ivAdd.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_PACK_CAR_UPDATE).withSerializable("packEntity",PackEntity()).navigation()

        }

        pAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RoutePath.PATH_PACK_CAR_UPDATE).withSerializable("packEntity",pAdapter.data.get(position)).navigation()
        }



        binding.search.ivLd.setOnClickListener {
            val searchStr = binding.search.etSearch.text.trim().toString()
            searchMethod(searchStr)
        }

        binding.search.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if(pList.size>0){
                    searchMethod(s.toString())
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })


        //友盟测试，后期删除
        binding.include.tvTitle.setOnClickListener {
            var web: UMWeb? = UMWeb("https://www.baidu.com")
            web!!.title = "This is music title" //标题
            val image = UMImage(this, "http://10.161.9.80:8080/gift/2.jpg") //网络图片
            web!!.setThumb(image) //缩略图
            web!!.description = "my description" //描述
            ShareAction(this)
                .withMedia(web)
                .setPlatform(SHARE_MEDIA.QZONE)
                .open()
        }




    }

    override fun initData() {


        vm.packSuccess.observe(this) {
            pAdapter.data.clear()
            pAdapter.data.addAll(it.reversed())
            pList.addAll(pAdapter.data)
            pAdapter.notifyDataSetChanged()
        }


        pAdapter.setOnItemLongClickListener { adapter, view, position ->
            val entity = pAdapter.data[position]
            deletePosition = position
            vm.deletePackById(entity.id)
            true
        }


        vm.deletePackSuccess.observe(this){
            if(deletePosition!=-1){
                pAdapter.notifyItemRemoved(deletePosition)
                pAdapter.data.removeAt(deletePosition)
                deletePosition = -1
            }
            ToastUtils.showShort("删除成功")
        }
    }


    fun searchMethod(searchStr: String) {

        val filter = pList.filter { packEntity ->
            packEntity.areaName.contains(searchStr) ||
                    packEntity.spaceType.contains(searchStr) ||
                    packEntity.spaceNumber.contains(searchStr) ||
                    packEntity.spaceStatus.contains(searchStr) ||
                    packEntity.licensePlate.contains(searchStr) ||
                    packEntity.userName.contains(searchStr)

        }



        pAdapter.data.clear()
        pAdapter.data.addAll(filter)
        pAdapter.notifyDataSetChanged()
    }

}